﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Models;
using BLL;
using System.IO;

namespace Dryricenet.houTai
{
    public partial class food_Add : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Xlk();
            }
        }
        //下拉框
        public void Xlk()
        {
            DropDownList1.DataSource = foodTypeBLL.liXlkBLL();
            DropDownList1.DataTextField = "FoodTypeName";
            DropDownList1.DataValueField = "FoodTypeID";
            DropDownList1.DataBind();
        }
        //提交
        protected void Add_Button_Click(object sender, EventArgs e)
        {
            string name = Session["UserName"].ToString();
            DataTable dt = userBLL.selectNameBll(name);
            DataRow dr = dt.Rows[0];//第一条数据
           int id = Convert.ToInt32(dr["UserID"]);
            if (txtName.Text.Trim() != "" && txtDJ.Text.Trim() != "" && txtKC.Text.Trim() != "" && txtNR.Text.Trim() != "" && txtXS.Text.Trim() != ""&& txtDW.Text.Trim()!="")
            {
                    Food f = new Food();
                    f.FoodName = txtName.Text.Trim();
                    f.FoodTypeID = DropDownList1.SelectedIndex + 1;
                    if (FileUpload1.HasFile)
                    {
                        if (CheckFileType(FileUpload1.FileName))
                        {
                            string fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + Path.GetExtension(FileUpload1.FileName);
                            string filePath = "~/image/" + fileName;
                            FileUpload1.SaveAs(MapPath(filePath));
                            f.PicUrl = filePath;
                        }
                    }
                    f.Price = Convert.ToDouble(txtDJ.Text.Trim());
                    f.Fen = txtDW.Text.Trim();
                    f.SalesVolume = Convert.ToInt32(txtXS.Text.Trim());
                    f.Inventory = Convert.ToInt32(txtKC.Text.Trim());
                    f.Special = drTJ.SelectedValue;
                    f.Describe = txtNR.Text.Trim();
                    f.Time = DateTime.Now;
                f.UserID = id;
                if (FileUpload1.FileName != "")
                {
                    if (foodBLL.foodAddBLL(f))
                    {
                        Response.Write("<script>alert('添加成功！')</script>");
                    }
                    else
                    {
                        Response.Write("<script>alert('添加失败！')</script>");
                    }
                }
                else
                {
                    Response.Write("<script>javascript:alert('您还没有上传图片!');history.back();</script>");
                    Response.End();
                }
                
            }
            else {
                Response.Write("<script>alert('请将内容填写完整！')</script>");
            }
        }
        public bool CheckFileType(string filename)
        {
            string ext = Path.GetExtension(filename);
            switch (ext.ToLower())
            {
                case ".gif": return true;
                case ".png": return true;
                case ".jpg": return true;
                case ".jpeg": return true;
                default: return false;
            }
        }
    }
}